<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/10
 * Time: 11:00
 */

namespace app\billiards\controller;

use app\billiards\model\Version;
use app\billiards\model\Videolist;
use think\Request;

class Video extends BassController{

    //视频列表展示
    public function video_list(){
        $arrWhere = [];
            $source = request()->get('source');
            $status = request()->get('status');
            $title = request()->get('title');
            if(!empty($source)){
                $arrWhere['source'] = $source;
            }
            if(!empty($status)){
                $arrWhere['status'] = $status;
            }
            if(empty($title)){
                $title = '';
            }
        $video_list = Videolist::where('status','<>',3)->where('title','like','%'.$title.'%')->where($arrWhere)->order('id','desc')->paginate(5,false,[ 'query' => $arrWhere]);
        //获取分页显示
        return view('',['video_list'=>$video_list,'source'=>$source,'status'=>$status,'title'=>$title]);
    }


    //视频添加
    public function video_add(){
        if(request()->isPost()){
            $data = request()->post();
            $data['ctime'] = time();
            //用户头像
            $user_photo = uploadImageOne('user_photo');
            //封面
            $photo = uploadImageOne('photo');
            if($user_photo['status']==0 && $photo['status']==0){
                $data['photo'] = $photo['message'];
                $data['user_photo'] = $user_photo['message'];
            }

            //插入数据
            $video = Videolist::create($data);
            if($video->id){
                $this->success('添加成功','admin/video/video_list');
            }else{
                $this->error('添加失败','admin/video/video_list');
            }
        }
        return view();
    }

    //视频修改
    public function video_edit(){
        $id = request()->get('id');
        $video_list = Videolist::get($id);
        if(request()->isPost()){
            $data = request()->post();
            $data['ctime'] = time();
            //用户头像
            $user_photo = uploadImageOne('user_photo');
            //封面
            $photo = uploadImageOne('photo');
            if($user_photo['status']==0 ){

                $data['user_photo'] = $user_photo['message'];
            }
            if($photo['status']==0){
                $data['photo'] = $photo['message'];
            }
            //插入数据
            $video = $video_list->save($data,['id' => $id]);
            if($video){
                $this->success('修改成功','admin/video/video_list');
            }else{
                $this->error('修改失败','admin/video/video_list');
            }
        }
        return view('',['video_list'=>$video_list]);
    }

    //删除视频
    public function del(){
       $id = request()->get('id');
       $video_obj = Videolist::get($id);
       $video_obj->status = 3;
       $res = $video_obj->save();
       if($res){
           $this->success('删除成功','admin/video/video_list');
       }else{
           $this->error('删除失败','admin/video/video_list');
       }
    }

    //屏蔽和取消屏蔽
    public function shield(){
        $id = request()->get('id');
        $video_obj = Videolist::get($id);
        if($video_obj->status==2){
            $video_obj->status = 0;
        }else{
            $video_obj->status = 2;
        }
        $res = $video_obj->save();
        if($res){
            $this->success('操作成功','admin/video/video_list');
        }else{
            $this->error('操作失败','admin/video/video_list');
        }
    }
}